<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="cabinetFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="故障类型" prop="objectStyle">
        <el-select v-model="drawerProps.row!.objectStyle" placeholder="请选择故障类型" clearable @change="_selFaultType">
          <el-option v-for="item in faultType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="故障名称" prop="commandId">
        <el-select
          v-model="drawerProps.row!.commandId"
          placeholder="请选择故障名称"
          clearable
          :disabled="!drawerProps.row!.objectStyle"
        >
          <el-option v-for="item in faultSelList" :key="item.commandId" :label="item.dataName" :value="item.commandId" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="CabineDrawer">
import { ref, reactive } from "vue";
import { faultType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { ResultData, TrainQuestion } from "@/api/interface";
import { getFaultSelList } from "@/api/modules/questionBank1Manage";
const faultSelData: any = ref({});
const faultSelList: any = ref([]);
const rules = reactive({
  objectStyle: [{ required: true, trigger: "blur", message: "请选择装置类型" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<TrainQuestion.ResTrainQuestionList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  const { result } = (await getFaultSelList({})) as ResultData;
  faultSelData.value = result;
};

// 提交数据（新增/编辑）
const cabinetFormRef = ref<FormInstance>();

const _selFaultType = (key: any) => {
  faultSelList.value = faultSelData.value[key];
};

/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  cabinetFormRef.value && cabinetFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  cabinetFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!({ ...drawerProps.value.row });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
defineExpose({
  acceptParams
});
</script>
